<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
class Class_M_DELIVE extends Class_M_DAO {
	//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table  = "M_DELIVE";
	
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey   = "id";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = "delive_cd";
	 
	 protected $_cols  = array(
	 	"matrix_cd"   => array(AOKI_VARTYPE_INT),
		"delive_cd"   => array(AOKI_VARTYPE_STR),
		"delive_name" => array(AOKI_VARTYPE_STR),
		"ins_co_cd"   => array(AOKI_VARTYPE_STR),
		"close_day"   => array(AOKI_VARTYPE_INT),
		"close_time"  => array(AOKI_VARTYPE_INT),
		"delive_name" => array(AOKI_VARTYPE_STR),
		"postcode"    => array(AOKI_VARTYPE_STR),
		"address"     => array(AOKI_VARTYPE_STR),
		"tel"     => array(AOKI_VARTYPE_STR),
		
	 );
	
	 protected $_checks  = array(
		"matrix_cd"      => array( "EXIST","NUM" ),
		"delive_cd"      => array( "EXIST" ),
		"delive_name"    => array( "EXIST" ),
		"ins_co_cd"      => array( "EXIST" ),
		"close_day"      => array( "NUM" ),
		"close_time"     => array( "NUM" ),
	 );
	 
	 //maxlength 
	 protected $_maxlens  = array(
		"delive_cd "     =>  5 ,
		"delive_name "   => 100 
	 );
	
	 public function search($where ,&$page = NULL ){	
	 	
		$sql = "SELECT a.*,(select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name from {$this->_table}  a ";
		return  parent::search($where ,$page ,$sql);
	}
	
	public function copy($where, $tWeek ){
		$loginuser = $_SESSION['ADMIN_LOGIN']['STAFF_SEQ'];
		
		$sqlSel = " SELECT {$tWeek},delive_cd,delive_name,ins_co_cd,close_day,close_time,postcode,address,tel," .
				"CURRENT_TIMESTAMP,'{$loginuser}' ".",CURRENT_TIMESTAMP,'{$loginuser}' from {$this->_table}   ";
		if ($where) {
			$where = implode(' AND ', $where);
			$sqlSel     = $sqlSel . " WHERE ".$where ;
		}
		
		$sql = " insert into {$this->_table}(matrix_cd,delive_cd,delive_name,ins_co_cd,close_day,close_time,postcode,address,tel,u_time,u_user,c_time,c_user) ";
		$sql = $sql.$sqlSel;
	
	 	$sqlDel = "delete from  {$this->_table}  where matrix_cd = {$tWeek}";
		$db = $this->_db;
		$this->begin_trans();
		try {
			$updateFlg = true;
			if (!$db->query( $sqlDel )) {
				$updateFlg = false;
			}
			if ($updateFlg) {
				if (!$db->query( $sql )) {
					$updateFlg = false;
				}
			}
			if ( $updateFlg ){
				$this->commit_trans();
				return true;
			} else {
				$this->rollback_trans();
				return false;
			}
		} catch (Exception $e) {
				echo "例外キャッチ：", $e->getMessage(), "\n";
				$this->rollback_trans();
				return false;
			}
	}
}

?>